package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func LoginAuth(c *gin.Context) {
	fmt.Println("我是登录拦截保护中间件")
}
func main() {
	router := gin.Default()
	// 全局只用中间件，所有的调用都会被执行
	//router.Use(LoginAuth)

	// 指定组添加拦截中间件
	goodsGroup := router.Group("/goods", LoginAuth)
	{
		goodsGroup.GET("", goodsList)
		goodsGroup.GET("/:id/:action/add", goodsDetail) //获取商品id为1的详细信息 模式
		goodsGroup.POST("", createGoods)
	}

	// 单个handler添加middleware
	router.POST("/user", LoginAuth, createGoods)

	router.Run(":8083")
}

func createGoods(c *gin.Context) {

}

func goodsDetail(c *gin.Context) {
	id := c.Param("id")
	action := c.Param("action")
	c.JSON(http.StatusOK, gin.H{
		"id":     id,
		"action": action,
	})
}

func goodsList(context *gin.Context) {
	context.JSON(http.StatusOK, gin.H{
		"name": "goodsList",
	})
}
